// 所有Api存放的位置
import axios from 'axios';
import qs from 'qs'


// 后台地址

//默认设置
const instance = axios.create({
    // baseURL: process.env.BASE_API,
    baseURL: '/api',
    timeout: 30000,
    headers: {
      'Content-Type': 'application/json;charset=UTF-8'
    },
    withCredentials:true,
});
  
  
// Request 拦截器
instance.interceptors.request.use(
    config => {
        let token = sessionStorage.getItem('token');
        if(token) {
        // 向请求头添加token
        config.headers['token'] = token;
        // console.log(config.headers['Authorization']);
        }
        return config
    },
    error => {
        return Promise.reject(error)
    }
);

// Response 拦截器
instance.interceptors.response.use(
    response => {
        return response
    },
    error => {
        return Promise.reject(error);
    }
);


const get = (url, params = {}) => {
 return new Promise((resolve, reject) => {

    instance.get(url, {params}).then(response => {

    resolve(response.data || {});

    }).catch(err => {
    reject(err);
    })
 })
};


const post = (url, params = {}) => {
 return new Promise((resolve, reject) => {
    params = params || {};
    instance({
        url:url,
        method:'post',
        data:params
    }).then(res=>{
        resolve(res.data || {}); 
    }).catch(err=>{
        reject(err);
    });
    
 })
};


export default {
    // 登录
    login(params){
        return post('/dp/portal/login',params)
    },
    // 注册
    regist(params){
        return post('/dp/portal/regist',params)
    },
    // 获取图片验证码
    getcaptcha(){
        return get('/dp/portal/getcaptcha')
    },
    // 获取通知公告
    getnotice(params){
        return get('/dp/report/GetNotice',params)
    },
    //提交成果
    postach(params){
        // return post(,params)
        return post('',params)
    },
    //获取成果信息
    getach(params){
        return get('/dp/report/GetAchives',params)
    },
    //获取最近成果列表信息
    getinfoBydate(params){
        return get("",params)
    },
    //获取需求
    getreq(params){
        return get('/dp/report/GetRequires',params)
    },
    //提交需求
    postreq(params){
        return post('',params)
    },
    //获取专家
    getexp(params){
        return get('dp/report/GetExperts',params)
    },
    //获取统计数据
    getcount(){
        return get('dp/report/GetStas')
    },
    // 获取机构
    getorganization(params){
        return get('dp/report/GetOrganizations',params)
    },
    // 获取政策
    getpolicys(params){
        return get('dp/report/GetPolicys',params)
    }
}
